using UnityEngine;
using System.Collections;

public class AggroState : MonoBehaviour {
	
	//attack stats
	public float attackSpeed = 1f;
	public int attackDamage = 79;
	public float attackRange = 1f;
	public float attackDelay = 10f;
	public float attackAnimationDelay = 0.5f;
	public float attackAnimationSpeed = 1f;
	private bool attackAnim = false;

	//movement
	public float turnSpeed = 1f;
	public float speed = 0.08f;
	public float maxVelocityChange = 1.0f;
	
	public float pursuitLength = 3f;	
	
	public float animationSpeed = 0.5f;
	
	private GameObject target;
	
	//used for timer
	private float attackTime = 0f;
	private float pursuitTime = 0f;
	private bool quitPursuit = false;
	private float attacked = 0f;

	
	public void setTarget(GameObject target){
	this.target = target;
	}
	
	void Start(){
		transform.parent.GetComponentInChildren<Animation>()["bite"].speed = attackAnimationSpeed;
	}


	public void setPursuit(){
		quitPursuit = false;
	}
	// Update is called once per frame
	void Update () {
		
		if(target != null){
			//Move!
			Quaternion rotation = Quaternion.LookRotation(target.transform.position-transform.parent.position,Vector3.up);
			transform.parent.rotation = Quaternion.Slerp(transform.parent.rotation,rotation,Time.deltaTime*turnSpeed);
			Vector3 direction = (target.transform.position-transform.position).normalized;
			
			
			Vector3 targetVelocity = new Vector3(0,0,1);
			targetVelocity *= speed;
			Vector3  velocity = transform.parent.rigidbody.velocity;
			Vector3 velocityChange = (targetVelocity-velocity);
			velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
		    velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
		    velocityChange.y = 0;
			
			//transform.parent.rigidbody.AddForce(direction*speed, ForceMode.VelocityChange);
			if(Time.time>attacked){
				transform.parent.rigidbody.MovePosition(transform.parent.position+transform.parent.forward*speed);
				transform.parent.GetComponentInChildren<Animation>().CrossFade("Take 001");
				//transform.parent.GetComponentInChildren<SnakeTurnHead>().restoreRotation();
			}
			if(Time.time>(attacked-attackDelay+attackAnimationDelay)&&attackAnim){
				target.networkView.RPC("DamageRPC",RPCMode.All,attackDamage,(target.transform.position-transform.position).normalized);
				attackAnim = false;
			}
		}
		if((target.transform.position-transform.position).magnitude<attackRange&&Time.time>attackTime){
			
			attacked = Time.time+attackDelay;
			attackTime = Time.time + attackSpeed;
			//target.GetComponent<Damagable>().Damage(attackDamage,Vector3.up,Vector3.up);
			transform.parent.GetComponentInChildren<Animation>().CrossFade("bite");
			//transform.parent.GetComponentInChildren<SnakeTurnHead>().rotateTowards(target.transform);
			attackAnim=true;
		}
		//Debug.Log("Time :"+ Time.time + " Pursuit Time: " + pursuitTime + "pursuit: " + quitPursuit);
		if(Time.time>pursuitTime&&quitPursuit){
			target = null;
			this.enabled = false;
			transform.parent.GetComponentInChildren<Animation>()["Take 001"].speed = 0.5f;
			networkView.RPC("setAnimationSpeedRPC",RPCMode.Others,animationSpeed);
			gameObject.GetComponent<AIcontroller>().enabled = true;
		}
		
	}
	
	void OnTriggerExit(Collider other){
		if(other.gameObject == target){
			pursuitTime = Time.time+pursuitLength;
			quitPursuit = true;
		}
	}
	
	[RPC]
	void setAnimationSpeedRPC(float speed){
		transform.parent.GetComponentInChildren<Animation>()["Take 001"].speed = 0.5f;
	}
}
